// lib/core/supabase_service.dart
import 'package:flutter/foundation.dart';
import 'package:supabase_flutter/supabase_flutter.dart';

class SupabaseService {
  // 单例模式确保全局唯一实例
  static final SupabaseService _instance = SupabaseService._internal();
  factory SupabaseService() => _instance;
  SupabaseService._internal();

  bool _isInitialized = false;

  /// 初始化Supabase连接
  static Future<void> init() async {
    try {
      if (_instance._isInitialized) return;

      // 配置核心参数
      await Supabase.initialize(
        url: 'https://mqjdjoxscqogjtjzffmo.supabase.co', // 替换你的URL
        anonKey: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im1xamRqb3hzY3FvZ2p0anpmZm1vIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDU4OTMyNTgsImV4cCI6MjA2MTQ2OTI1OH0.wZ3N2GY7SJelB6nlfIhDFpwT5CsVkgv3hf0XhrHVjFg', // 替换你的Anon Key
      );

      _instance._isInitialized = true;
      debugPrint('✅ Supabase初始化成功');
    } catch (e, stack) {
      debugPrint('❌ 初始化失败: $e\n$stack');
      rethrow;
    }
  }

  /// 全局访问入口
  static SupabaseClient get client {
    if (!_instance._isInitialized) {
      throw Exception('Supabase未初始化，请先调用init()');
    }
    return Supabase.instance.client;
  }
}